1. Field of the Invention
The present invention relates, in general, to data processing, and, more particularly, to thread synchronization in JAVA language programs.
2. Relevant Background
The JAVA(trademark) (a trademark of Sun Microsystems, Inc.) programming language, is an object-oriented programming language developed by Sun Microsystems, Inc., the Assignee of the present invention. The JAVA programming language has found success as a language and programming environment for networked applications. The JAVA programming language is attractive due to its many features, including standardized support for concurrent execution of program threads. The JAVA programming language""s concurrency features are provided at both a language (syntactic) level and through a xe2x80x9cthreadsxe2x80x9d library. At the language level, an object""s methods can be declared xe2x80x9csynchronizedxe2x80x9d. Methods within a class that are declared synchronized do not run concurrently and run under control of xe2x80x9cmonitorsxe2x80x9d to ensure that variables remain in a consistent state.
Each time a synchronized method is entered or exited, the JAVA language requires calls to the operating system (O/S) kernel to allocate thread synchronization resources. Calls to the kernel may require tens if not hundreds of instructions depending on the O/S in use. In comparison, the synchronized method itself may require only a few lines of code. As an example, a dictionary hash table method can be implemented with fewer than ten instructions, but to implement it as a synchronized method requires more than 100 instruction in a typical operating system. Hence, thread synchronization significantly adds to the execution time of many programs.
This overhead is required in programs that make heavy use of multi-threading and depend on thread synchronization. However, this overhead is undesirable in programs that are single-threaded. Similarly, even in multithreaded programs, a large number of the threads may in fact execute correctly without the synchronization overhead. Hence, a need exists for a thread synchronization mechanism that only incurs the overhead associated with O/S thread management resource allocation only when those resources are needed.
Operating systems conventionally enable multithreading in one of two ways: preemptable and non-preemptable. A preemptable thread operating system (e.g., Solaris and Windows/NT) include O/S techniques and devices that enable one thread to interrupt another concurrently executing thread. Hence, at any given time, an executing thread cannot predict whether it will continue to execute or whether it will be blocked by another thread. Hence, the application cannot manage thread synchronization on its own because it lacks visibility as to when threads will be blocked. Preemptable threads are also valuable in multiprocessing machines to efficiently distribute execution of threads across multiple processors.
Non-preemptable multithreading is an simpler form of multithreading that supports a mode of thread execution, whereby once a thread begins to execute, it cannot be blocked by another thread. A thread may halt or block itself, yield control to other threads, or be blocked by virtue of waiting for input/output (I/O) to complete. There remain a large number of applications that can be implemented as single threads and which do not require the preemptive multithreading features of an operating system. Non-preemptive operating systems will likely exist in information appliances and simpler operating systems for some time. Where the O/S ensures that each thread cannot be preempted, allocation of thread synchronization resources in the O/S increases program execution time with little benefit.
Multithreading takes advantage of parallelism inherent in (or designed into) many programs. However, legacy programs often exhibit little parallelism. Moreover, some programs by the nature of their behavior do not exhibit a high degree of parallelism. These programs are slowed by having to incur the overhead associated with multithreading operating systems without reaping any benefits because of their non-parallel structure. Hence, a need exists for a thread synchronization mechanism that speeds up execution in programs that are essentially un-threaded yet running on a multithreading operating system.
Briefly stated, the present invention involves a method for synchronized thread execution in a multithreaded processor. Each synchronized thread refers to at least one object identified by an object identification (OID) that is shared among a plurality of synchronized threads. One of the synchronized threads is selected for execution. Upon entering the selected thread, an entry sequence indicates that the shared object should be locked by pushing its OID onto a lock stack. The operations defined by the selected thread are executed and the indication is removed by pushing the OID from the lock stack.
The foregoing and other features, utilities and advantages of the invention will be apparent from the following more particular description of preferred embodiments of the invention as illustrated in the accompany drawings. Still other embodiments of the present invention will become readily apparent to those skilled in the art from the following detailed description, wherein is shown and described only the embodiments of the invention by way of illustration of the best modes contemplated for carrying out the invention. As will be appreciated, the invention is capable of other and different embodiments and several of its details are capable of modification in various obvious respects, all without departing from the spirit and scope of the present invention. Accordingly, the drawings and detailed description are to be regarded as illustrative in nature and not as restrictive.